package com.xiaoyu.client.ui.activity.main.mine.my_order;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.order.OrderBean;
import com.xiaoyu.client.model.order.OrderDetailsBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.PreferencesUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author jx
 * @date 创建时间 2018/10/24
 * @Description 订单详情
 */
public class ExamineOrderActivity extends BaseActivityWithTitle {

    private String orderId;
    @BindView(R.id.activity_examine_order_name)
    TextView mNameMobile;
    @BindView(R.id.activity_examine_order_address_txt)
    TextView mAddress;
    @BindView(R.id.activity_examine_order_store)
    TextView mStoreName;
    @BindView(R.id.activity_examine_order_store_img)
    ImageView mStoreImg;
    @BindView(R.id.activity_examine_order_goods_name)
    TextView mGoodsName;
    @BindView(R.id.activity_examine_order_goods_num)
    TextView mGoodsNum;
    @BindView(R.id.activity_examine_order_goods_price)
    TextView mGoodsPrice;
    @BindView(R.id.activity_examine_ordernumber)
    TextView mOrderNumber;
    @BindView(R.id.activity_examine_orderaddtime)
    TextView mOrderAddTime;
    @BindView(R.id.activity_examine_paytype)
    TextView mPayType;
    @BindView(R.id.activity_examine_logisticsnumber)
    TextView mLogisticsNumber;
    @BindView(R.id.activity_examine_wares_retal)
    TextView mGoodsAllPrice;
    @BindView(R.id.activity_examine_goodsfreight)
    TextView mGoodsFreight;
    @BindView(R.id.activity_examine_order_money)
    TextView mOrderMoney;

    @BindView(R.id.activity_examine_order_linear)
    LinearLayout mOrderPayLinear;
    @BindView(R.id.activity_examine_order_line)
    View mLine;
    @BindView(R.id.del_order)
    TextView mDelOrder;
    @BindView(R.id.examine_order_apply_for_after_sales)
    TextView mApplyAfter;

    @Override
    protected void loadContentView(ViewGroup parent) {
        setTitleText("订单详情");
        View mView = LayoutInflater.from(this).inflate(R.layout.activity_examine_order, parent, true);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

        if (getIntent().getStringExtra(OrderFragment.ORDER_DETAILS).equals("order0")) {//待付款
            mOrderPayLinear.setVisibility(View.GONE);
            mDelOrder.setVisibility(View.VISIBLE);
            mApplyAfter.setVisibility(View.GONE);
        } else if (getIntent().getStringExtra(OrderFragment.ORDER_DETAILS).equals("order1")) {//待发货
            mLine.setVisibility(View.GONE);
            mLogisticsNumber.setVisibility(View.GONE);
            mDelOrder.setVisibility(View.GONE);
            mApplyAfter.setVisibility(View.VISIBLE);
        } else if (getIntent().getStringExtra(OrderFragment.ORDER_DETAILS).equals("order2")) {//待收货
            mDelOrder.setVisibility(View.GONE);
            mApplyAfter.setVisibility(View.VISIBLE);
        } else if (getIntent().getStringExtra(OrderFragment.ORDER_DETAILS).equals("order3")) {//待评价
            mDelOrder.setVisibility(View.VISIBLE);
            mApplyAfter.setVisibility(View.GONE);
        } else if (getIntent().getStringExtra(OrderFragment.ORDER_DETAILS).equals("order4")) {//已完成
            mDelOrder.setVisibility(View.VISIBLE);
            mApplyAfter.setVisibility(View.GONE);
        }

        OrderBean.DataBean.OrderParam orderParam = (OrderBean.DataBean.OrderParam) getIntent().getSerializableExtra(OrderFragment.ORDER_INFO);
        orderId = orderParam.getOrderid();
        orderDetails();
    }


    //申请售后
    @OnClick(R.id.examine_order_apply_for_after_sales)
    public void applyAfter() {
        startActivity(new Intent(this, SelectServiceTypeOneActivity.class));
    }

    @OnClick(R.id.del_order)
    public void delDorderClick() {
        NetworkManager.delOrder(orderId, PreferencesUtil.getString(PreferencesUtil.TOKEN, ""), new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                ToastUtil.showToast(ExamineOrderActivity.this, "订单删除成功");
                finish();
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    //获取订单详情页面数据
    private void orderDetails() {

        NetworkManager.orderDetails(orderId, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                succeedResult(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    //解析显示订单数据
    private void succeedResult(String result) {
        OrderDetailsBean bean = new Gson().fromJson(result, OrderDetailsBean.class);
        mNameMobile.setText(bean.getData().getOrdername() + "   " + bean.getData().getTel());
        mAddress.setText(bean.getData().getAddress());
        mStoreName.setText(bean.getData().getGoodsstore().getStorename());
        Glide.with(this).load(bean.getData().getDetail().getGoodsdeimg()).into(mStoreImg);
        mGoodsName.setText(bean.getData().getDetail().getGoodsname());
        mGoodsNum.setText("数量：" + bean.getData().getDetail().getGoodsnumber());
        mGoodsPrice.setText("￥ " + bean.getData().getDetail().getGoodsprice());
        mOrderNumber.setText("订单编号：" + bean.getData().getOrdernumber());
        mOrderAddTime.setText("下单时间：" + bean.getData().getOrderaddtime());
        mPayType.setText("支付方式：" + bean.getData().getPaytype());
        mLogisticsNumber.setText("快递单号：" + bean.getData().getLogisticsnumber());
        mGoodsAllPrice.setText("￥ " + bean.getData().getGrossprice());
        if (bean.getData().getGoodsfreight().equals("0")) {
            mGoodsFreight.setText("包邮");
        } else {
            mGoodsFreight.setText("+ " + bean.getData().getGoodsfreight());
        }
        mOrderMoney.setText(bean.getData().getOrdermoney());
    }

}
